package com.qidianit.mes.production.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.common.pojo.MESResult;
import com.qidianit.common.util.Constant;
import com.qidianit.mes.pojo.ProcessSteps;
import com.qidianit.mes.pojo.WorkmanshipComposition;
import com.qidianit.mes.production.mapper.WorkmanshipCompositionMapper;
import com.qidianit.mes.production.service.WorkmanshipCompositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author djl
 * @Time 2024/10/21 11:13
 */
@Service
public class WorkmanshipCompositionServiceImpl implements WorkmanshipCompositionService {
    @Autowired
    private WorkmanshipCompositionMapper workmanshipCompositionMapper;

    @Override
    public MESResult list(Integer workmanshipId, int page, int limit) {
        System.out.println(workmanshipId);
        // 开启分页  参数1： 页码  参数2： 每页大小
        PageHelper.startPage(page,limit);
        // 查询全部
        List<WorkmanshipComposition> workmanshipCompositions = workmanshipCompositionMapper.listByWorkmanshipId(workmanshipId);
        PageInfo<WorkmanshipComposition> pageInfo = new PageInfo<>(workmanshipCompositions);
        // 处理列表，设置nextProcess字段
        for (int i = 0; i < pageInfo.getList().size() - 1; i++) {
            WorkmanshipComposition current = pageInfo.getList().get(i);
            WorkmanshipComposition next = pageInfo.getList().get(i + 1);

            // 设置当前元素的nextProcess为下一个元素的processName
            current.setNextProcess(next.getProcess().getProcessName());
        }

        // 最后一个元素的nextProcess应该为空或特殊值，取决于业务需求
        if (!pageInfo.getList().isEmpty()) {
            WorkmanshipComposition last = pageInfo.getList().get(pageInfo.getList().size() - 1);
            last.setNextProcess(null); // 或者设置为某个特定值表示没有下一个流程
        }
        System.out.println("-------------------------");
        System.out.println(pageInfo);
        // 将pageInfo对象设置到qdResult中
        return MESResult.success(pageInfo);
    }

    @Override
    public MESResult addNewComposition(WorkmanshipComposition workmanshipComposition) {
        if(workmanshipCompositionMapper.selectByComposition(workmanshipComposition.getWorkmanshipId(),workmanshipComposition.getProcessNumber()) == null){
            workmanshipCompositionMapper.addNewCom(workmanshipComposition);
            return MESResult.success(Constant.RESPONSE_STATUS_OK);
        }
        return MESResult.error(Constant.RESPONSE_STATUS_REPEAT);
    }

    @Override
    public MESResult editComposition(WorkmanshipComposition workmanshipComposition) {
        if(workmanshipCompositionMapper.selectByComposition(workmanshipComposition.getWorkmanshipId(),workmanshipComposition.getProcessNumber()) == null){
            workmanshipCompositionMapper.editCom(workmanshipComposition);
            return MESResult.success(Constant.RESPONSE_STATUS_OK);
        }else if(workmanshipCompositionMapper.selectByComposition(workmanshipComposition.getWorkmanshipId(),workmanshipComposition.getProcessNumber()).getWorkmanshipCompositionId() == workmanshipComposition.getWorkmanshipCompositionId()){
            workmanshipCompositionMapper.editCom(workmanshipComposition);
            return MESResult.success(Constant.RESPONSE_STATUS_OK);
        }
        return null;
    }

    @Override
    public MESResult query(Integer workmanshipId, Integer processNumber) {
        return MESResult.success(workmanshipCompositionMapper.selectByComposition(workmanshipId,processNumber));
    }
}
